package com.xinyue.auto.reader.process;

import java.util.List;

import org.dom4j.Element;

import com.xinyue.auto.exception.OneException;
import com.xinyue.auto.reader.Process;
import com.xinyue.auto.reader.major.RelyMajor;
import com.xinyue.auto.reader.util.SetterUtil;
import com.xinyue.auto.util.StringUtil;

public class OneProcess extends RelyMajor{
	public OneProcess(Process read) {
		super(read);
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void validate() throws Exception {
		//读取one标签
		List<Element> one = xml.elements(one_);
		if(one != null && one.size() > 0) {
			for (int i = 0; i < one.size(); i++) {
				try {
					//公共属性
					commonRead(one.get(i));
					att.setType(entity);
					
					//添加属性
					atts.add(att);
					
					//预处理查询方法
					process();
					//添加查询方法
					querys.add(query);
				} catch (Exception e) {
					throw new OneException(e.getMessage(), holder.getException());
				}
			}
		}
	}
	
	@Override
	protected void process() {
		query.setReturnType(entity);
		query.setMethod("query" + entity + "By" + StringUtil.firstUpper(key));
		query.setParams(queryType + " " + key);
		query.setState("from " + entity + " where " + key + "=:" + key);
		
		String[] arr = new String[1];
		arr[0] = SetterUtil.setterHQL(key, queryType);
		query.setParamSetter(arr);
		
		query.setQuery(true);
		query.setConvert(true);
		query.setOnly(true);
	}
}